/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Dao.exceptions.IllegalOrphanException;
import Dao.exceptions.NonexistentEntityException;
import Dao.exceptions.PreexistingEntityException;
import Logico.Deportista;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Logico.Disciplina;
import java.util.ArrayList;
import java.util.List;
import Logico.DeportistaCompetencia;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author usuario
 */
public class DeportistaJpaController implements Serializable {

    public DeportistaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Deportista deportista) throws PreexistingEntityException, Exception {
        if (deportista.getDisciplinaList() == null) {
            deportista.setDisciplinaList(new ArrayList<Disciplina>());
        }
        if (deportista.getDeportistaCompetenciaList() == null) {
            deportista.setDeportistaCompetenciaList(new ArrayList<DeportistaCompetencia>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Disciplina> attachedDisciplinaList = new ArrayList<Disciplina>();
            for (Disciplina disciplinaListDisciplinaToAttach : deportista.getDisciplinaList()) {
                disciplinaListDisciplinaToAttach = em.getReference(disciplinaListDisciplinaToAttach.getClass(), disciplinaListDisciplinaToAttach.getIddisciplina());
                attachedDisciplinaList.add(disciplinaListDisciplinaToAttach);
            }
            deportista.setDisciplinaList(attachedDisciplinaList);
            List<DeportistaCompetencia> attachedDeportistaCompetenciaList = new ArrayList<DeportistaCompetencia>();
            for (DeportistaCompetencia deportistaCompetenciaListDeportistaCompetenciaToAttach : deportista.getDeportistaCompetenciaList()) {
                deportistaCompetenciaListDeportistaCompetenciaToAttach = em.getReference(deportistaCompetenciaListDeportistaCompetenciaToAttach.getClass(), deportistaCompetenciaListDeportistaCompetenciaToAttach.getDeportistaCompetenciaPK());
                attachedDeportistaCompetenciaList.add(deportistaCompetenciaListDeportistaCompetenciaToAttach);
            }
            deportista.setDeportistaCompetenciaList(attachedDeportistaCompetenciaList);
            em.persist(deportista);
            for (Disciplina disciplinaListDisciplina : deportista.getDisciplinaList()) {
                disciplinaListDisciplina.getDeportistaList().add(deportista);
                disciplinaListDisciplina = em.merge(disciplinaListDisciplina);
            }
            for (DeportistaCompetencia deportistaCompetenciaListDeportistaCompetencia : deportista.getDeportistaCompetenciaList()) {
                Deportista oldDeportistaOfDeportistaCompetenciaListDeportistaCompetencia = deportistaCompetenciaListDeportistaCompetencia.getDeportista();
                deportistaCompetenciaListDeportistaCompetencia.setDeportista(deportista);
                deportistaCompetenciaListDeportistaCompetencia = em.merge(deportistaCompetenciaListDeportistaCompetencia);
                if (oldDeportistaOfDeportistaCompetenciaListDeportistaCompetencia != null) {
                    oldDeportistaOfDeportistaCompetenciaListDeportistaCompetencia.getDeportistaCompetenciaList().remove(deportistaCompetenciaListDeportistaCompetencia);
                    oldDeportistaOfDeportistaCompetenciaListDeportistaCompetencia = em.merge(oldDeportistaOfDeportistaCompetenciaListDeportistaCompetencia);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findDeportista(deportista.getCedula()) != null) {
                throw new PreexistingEntityException("Deportista " + deportista + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Deportista deportista) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Deportista persistentDeportista = em.find(Deportista.class, deportista.getCedula());
            List<Disciplina> disciplinaListOld = persistentDeportista.getDisciplinaList();
            List<Disciplina> disciplinaListNew = deportista.getDisciplinaList();
            List<DeportistaCompetencia> deportistaCompetenciaListOld = persistentDeportista.getDeportistaCompetenciaList();
            List<DeportistaCompetencia> deportistaCompetenciaListNew = deportista.getDeportistaCompetenciaList();
            List<String> illegalOrphanMessages = null;
            for (DeportistaCompetencia deportistaCompetenciaListOldDeportistaCompetencia : deportistaCompetenciaListOld) {
                if (!deportistaCompetenciaListNew.contains(deportistaCompetenciaListOldDeportistaCompetencia)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain DeportistaCompetencia " + deportistaCompetenciaListOldDeportistaCompetencia + " since its deportista field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Disciplina> attachedDisciplinaListNew = new ArrayList<Disciplina>();
            for (Disciplina disciplinaListNewDisciplinaToAttach : disciplinaListNew) {
                disciplinaListNewDisciplinaToAttach = em.getReference(disciplinaListNewDisciplinaToAttach.getClass(), disciplinaListNewDisciplinaToAttach.getIddisciplina());
                attachedDisciplinaListNew.add(disciplinaListNewDisciplinaToAttach);
            }
            disciplinaListNew = attachedDisciplinaListNew;
            deportista.setDisciplinaList(disciplinaListNew);
            List<DeportistaCompetencia> attachedDeportistaCompetenciaListNew = new ArrayList<DeportistaCompetencia>();
            for (DeportistaCompetencia deportistaCompetenciaListNewDeportistaCompetenciaToAttach : deportistaCompetenciaListNew) {
                deportistaCompetenciaListNewDeportistaCompetenciaToAttach = em.getReference(deportistaCompetenciaListNewDeportistaCompetenciaToAttach.getClass(), deportistaCompetenciaListNewDeportistaCompetenciaToAttach.getDeportistaCompetenciaPK());
                attachedDeportistaCompetenciaListNew.add(deportistaCompetenciaListNewDeportistaCompetenciaToAttach);
            }
            deportistaCompetenciaListNew = attachedDeportistaCompetenciaListNew;
            deportista.setDeportistaCompetenciaList(deportistaCompetenciaListNew);
            deportista = em.merge(deportista);
            for (Disciplina disciplinaListOldDisciplina : disciplinaListOld) {
                if (!disciplinaListNew.contains(disciplinaListOldDisciplina)) {
                    disciplinaListOldDisciplina.getDeportistaList().remove(deportista);
                    disciplinaListOldDisciplina = em.merge(disciplinaListOldDisciplina);
                }
            }
            for (Disciplina disciplinaListNewDisciplina : disciplinaListNew) {
                if (!disciplinaListOld.contains(disciplinaListNewDisciplina)) {
                    disciplinaListNewDisciplina.getDeportistaList().add(deportista);
                    disciplinaListNewDisciplina = em.merge(disciplinaListNewDisciplina);
                }
            }
            for (DeportistaCompetencia deportistaCompetenciaListNewDeportistaCompetencia : deportistaCompetenciaListNew) {
                if (!deportistaCompetenciaListOld.contains(deportistaCompetenciaListNewDeportistaCompetencia)) {
                    Deportista oldDeportistaOfDeportistaCompetenciaListNewDeportistaCompetencia = deportistaCompetenciaListNewDeportistaCompetencia.getDeportista();
                    deportistaCompetenciaListNewDeportistaCompetencia.setDeportista(deportista);
                    deportistaCompetenciaListNewDeportistaCompetencia = em.merge(deportistaCompetenciaListNewDeportistaCompetencia);
                    if (oldDeportistaOfDeportistaCompetenciaListNewDeportistaCompetencia != null && !oldDeportistaOfDeportistaCompetenciaListNewDeportistaCompetencia.equals(deportista)) {
                        oldDeportistaOfDeportistaCompetenciaListNewDeportistaCompetencia.getDeportistaCompetenciaList().remove(deportistaCompetenciaListNewDeportistaCompetencia);
                        oldDeportistaOfDeportistaCompetenciaListNewDeportistaCompetencia = em.merge(oldDeportistaOfDeportistaCompetenciaListNewDeportistaCompetencia);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = deportista.getCedula();
                if (findDeportista(id) == null) {
                    throw new NonexistentEntityException("The deportista with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Deportista deportista;
            try {
                deportista = em.getReference(Deportista.class, id);
                deportista.getCedula();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The deportista with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<DeportistaCompetencia> deportistaCompetenciaListOrphanCheck = deportista.getDeportistaCompetenciaList();
            for (DeportistaCompetencia deportistaCompetenciaListOrphanCheckDeportistaCompetencia : deportistaCompetenciaListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Deportista (" + deportista + ") cannot be destroyed since the DeportistaCompetencia " + deportistaCompetenciaListOrphanCheckDeportistaCompetencia + " in its deportistaCompetenciaList field has a non-nullable deportista field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Disciplina> disciplinaList = deportista.getDisciplinaList();
            for (Disciplina disciplinaListDisciplina : disciplinaList) {
                disciplinaListDisciplina.getDeportistaList().remove(deportista);
                disciplinaListDisciplina = em.merge(disciplinaListDisciplina);
            }
            em.remove(deportista);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Deportista> findDeportistaEntities() {
        return findDeportistaEntities(true, -1, -1);
    }

    public List<Deportista> findDeportistaEntities(int maxResults, int firstResult) {
        return findDeportistaEntities(false, maxResults, firstResult);
    }

    private List<Deportista> findDeportistaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Deportista.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Deportista findDeportista(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Deportista.class, id);
        } finally {
            em.close();
        }
    }

    public int getDeportistaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Deportista> rt = cq.from(Deportista.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
